package com.golden.procedure.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.procedure.dao.WorkAttachmentMapper;
import com.golden.procedure.dao.WorkOrderMapper;
import com.golden.procedure.dao.WorkOrderPickMapper;
import com.golden.procedure.dao.WorkPhotoFilmMapper;
import com.golden.procedure.dao.WorkPhotoProductMapper;
import com.golden.procedure.domain.OrderProductDO;
import com.golden.procedure.domain.WorkAttachmentDO;
import com.golden.procedure.domain.WorkOrderDO;
import com.golden.procedure.domain.WorkPhotoPickDO;
import com.golden.procedure.domain.WorkPhotoProductDO;
import com.golden.procedure.domain.WorkPickDetailDO;
import com.golden.procedure.entity.WorkAttachment;
import com.golden.procedure.entity.WorkAttachmentExample;
import com.golden.procedure.entity.WorkOrder;
import com.golden.procedure.entity.WorkPhotoFilm;
import com.golden.procedure.entity.WorkPhotoFilmExample;
import com.golden.procedure.entity.WorkPhotoProduct;
import com.golden.procedure.entity.WorkPhotoProductExample;
import com.golden.procedure.query.WorkAttachmentQueryDO;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.ResultSupport;
import com.golden.procedure.service.IWorkAttachmentService;
import com.golden.procedure.service.IWorkOrderService;
import com.golden.procedure.service.IWorkPhotoProductService;
import com.golden.procedure.util.BeanUtilsExtends;
import com.golden.procedure.util.BonConstant;

@Service("workPhotoProductService")
public class WorkPhotoProductService extends BaseService implements IWorkPhotoProductService {

	@Autowired
	WorkPhotoProductMapper workPhotoProductMapper;
	@Autowired
	WorkOrderPickMapper workOrderPickMapper;
	@Autowired
	WorkAttachmentMapper workAttachmentMapper;
	@Autowired
	WorkPhotoFilmMapper workPhotoFilmMapper;
	@Autowired
	WorkOrderMapper workOrderMapper;
	@Autowired
	IWorkOrderService workOrderService;
	@Autowired
	IWorkAttachmentService workAttachmentService;

	/**
	 * 选片入册 <b>Description: 选片入册</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年9月20日 下午3:43:42 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO add(WorkPhotoPickDO domain) {

		ResultDO result = new ResultSupport();
		if (isNumberInValid(domain.getWorkOrderID()) || isNumberInValid(domain.getAttachmentID())) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		// 1.查询工单
		WorkOrder workOrder = workOrderMapper.selectByPrimaryKey(domain.getWorkOrderID());
		if (null == workOrder) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("工单信息查询失败");
			return result;
		}
		// 2.查询照片信息
		WorkAttachment attachment = workAttachmentMapper.selectByPrimaryKey(domain.getAttachmentID());
		if (null == attachment) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("照片信息查询失败");
			return result;
		}

		// 3.设置照片状态为 已选（修改照片入底数据表）
		WorkAttachmentDO attachmentDO = new WorkAttachmentDO();
		attachmentDO.setId(domain.getAttachmentID());
		attachmentDO.setBusinessNo(domain.getBusinessNo());
		attachmentDO.setStatus(BonConstant.STATUS_TRUE);
		attachmentDO.setDescription(domain.getComment());
		workAttachmentService.setStatus(attachmentDO);

		// 4.删除该照片的原有入册数据
		deleteByAttachmentID(domain.getAttachmentID(), domain.getBusinessNo());

		// 5.未选中入册商品，则返回
		if (null == domain.getProductList() || domain.getProductList().size() < 1) {
			return result;
		}

		// 6.有选中的商品，填充数据，入册
		for (OrderProductDO data : domain.getProductList()) {

			// 跳过未选：-1; 未选； 0-选中； 1-扉页
			if (data.getStatus() == BonConstant.STATUS_INVALID) {
				continue;
			}
			WorkPhotoProduct entity = new WorkPhotoProduct();
			entity.setAttachmentID(domain.getAttachmentID());
			entity.setWorkOrderPickID(domain.getWorkOrderPickID());
			entity.setWorkOrderID(domain.getWorkOrderID());
			entity.setBusinessNo(domain.getBusinessNo());
			entity.setComment(domain.getComment());
			entity.setOrderProductID(data.getOrderProductID());
			entity.setOrderProductName(data.getOrderProductName());
			entity.setStatus(data.getStatus()); // 2-选中； 3-扉页
			// 设置订单信息
			entity.setOrderID(workOrder.getOrderID());
			entity.setOrderProductSceenID(workOrder.getOrderProductSceenID());
			// 设置照片信息
			entity.setFileName(attachment.getFileName());
			entity.setThumbnailUrl(attachment.getThumbnailUrl());
			entity.setSource(attachment.getSource());
			entity.setUploadDate(attachment.getCreatetime());
			entity.setDescription(attachment.getDescription());

			// 照片入册扉页，需要删除该商品之前设置的扉页
			if (data.getStatus() == BonConstant.PRODUCT_PHOTO_COVER) {
				removeCover(data.getOrderProductID(), domain.getBusinessNo());
			}
			// 新入册
			entity.setIsDelete(BonConstant.DB_NORMAL);
			entity.setCreatetime(Calendar.getInstance().getTime());

			int ret = workPhotoProductMapper.insert(entity);
			if (ret < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("入册失败");
				result.setSuccess(false);
			}
		}
		return result;
	}

	@Override
	public ResultDO query(WorkAttachmentQueryDO queryDO) {

		ResultDO result = new ResultSupport();

		WorkPhotoProductExample example = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria criteria = example.createCriteria();
		if (!isNumberInValid(queryDO.getWorkOrderID())) {
			criteria.andWorkOrderIDEqualTo(queryDO.getWorkOrderID());
		}
		if (!isNumberInValid(queryDO.getOrderProductID())) {
			criteria.andOrderProductIDEqualTo(queryDO.getOrderProductID());
		}
		if (StringUtils.isNotEmpty(queryDO.getBusinessNo())) {
			criteria.andBusinessNoEqualTo(queryDO.getBusinessNo());
		}
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		example.setOrderByClause(" attachmentID asc ");

		List<WorkPhotoProduct> list = workPhotoProductMapper.selectByExample(example);

		List<WorkPhotoProductDO> doList = getWorkPhotoProductDOList(list);

		// 填充照片数据
		for (WorkPhotoProductDO data : doList) {
			WorkAttachment photo = workAttachmentMapper.selectByPrimaryKey(data.getAttachmentID());
			if (null == photo) {
				result.setSuccess(false);
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("照片查询失败");
				return result;
			}
			data.setThumbnailUrl(photo.getThumbnailUrl());
			data.setBigThumbnailUrl(photo.getBigThumbnailUrl());
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		// 查询商品信息（已入册数）
		result.setModel(ResultDO.SECOND_MODEL_KEY, doList.size());
		return result;
	}

	@Override
	public ResultDO queryPick(WorkAttachmentQueryDO queryDO) {
		ResultDO result = new ResultSupport();
		if (StringUtils.isEmpty(queryDO.getBusinessNo())) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		WorkPhotoFilmExample example = new WorkPhotoFilmExample();
		WorkPhotoFilmExample.Criteria criteria = example.createCriteria();
		// 工单ID
		if (!isNumberInValid(queryDO.getWorkOrderID())) {
			criteria.andWorkOrderIDEqualTo(queryDO.getWorkOrderID());
		}
		// 业务单号
		criteria.andBusinessNoEqualTo(queryDO.getBusinessNo());
		// 片源
		if (null != queryDO.getSource()) {
			criteria.andSourceEqualTo(queryDO.getSource());
		}
		// 片源
		if (null != queryDO.getSources() && queryDO.getSources().length > 0) {
			criteria.andSourceIn(Arrays.asList(queryDO.getSources()));
		}
		// 状态
		if (null != queryDO.getStatus() && queryDO.getStatus().length > 0) {
			criteria.andStatusIn(Arrays.asList(queryDO.getStatus()));
		}
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		int total = 0;
		if (queryDO.isPage()) {
			example.setLimitByPage(queryDO.getStart() + "," + queryDO.getPageRows());
			total = workPhotoFilmMapper.countByExample(example);
		}

		List<WorkPhotoFilm> list = workPhotoFilmMapper.selectByExample(example);

		// 查询入册照片列表
		WorkPhotoProductExample productExample = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria productCriteria = productExample.createCriteria();
		if (!isNumberInValid(queryDO.getWorkOrderID())) {
			productCriteria.andWorkOrderIDEqualTo(queryDO.getWorkOrderID());
		}
		productCriteria.andBusinessNoEqualTo(queryDO.getBusinessNo());
		productCriteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		List<WorkPhotoProduct> productList = workPhotoProductMapper.selectByExample(productExample);
		// 按照文件id进行分类
		Map<Integer, List<WorkPhotoProduct>> productMap = new HashMap<Integer, List<WorkPhotoProduct>>();
		for (WorkPhotoProduct product : productList) {
			if (productMap.containsKey(product.getAttachmentID())) {
				productMap.get(product.getAttachmentID()).add(product);
			} else {
				List<WorkPhotoProduct> ll = new ArrayList<WorkPhotoProduct>();
				ll.add(product);
				productMap.put(product.getAttachmentID(), ll);
			}
		}

		List<WorkAttachmentDO> doList = new ArrayList<WorkAttachmentDO>();
		// 查询未选照片数量(如果接口要分页，则未选照片数量需要重新查询）
		int notPickNum = 0;
		for (WorkPhotoFilm data : list) {
			if (data.getStatus() != BonConstant.STATUS_TRUE) {
				notPickNum++; // 统计未选照片数量
			}
			WorkAttachment entity = workAttachmentMapper.selectByPrimaryKey(data.getAttachmentID());
			WorkAttachmentDO attachmentDO = getWorkAttachmentDO(entity);
			if (null != attachmentDO) {
				attachmentDO.setAttachmentID(attachmentDO.getId());
				// 选中状态
				attachmentDO.setStatus(data.getStatus());
				// 业务单号
				attachmentDO.setBusinessNo(data.getBusinessNo());
				// 入底备注
				attachmentDO.setComment(data.getComment());
				attachmentDO.setProductNames(new String[] {});
				// 判断是否入册（添加入册商品及制作要求信息，并修改照片状态为2：入册）
				if (productMap.containsKey(entity.getId())) {
					String comment = "";
					List<String> productNames = new ArrayList<String>();
					for (WorkPhotoProduct product : productMap.get(entity.getId())) {
						// 制作要求
						comment = product.getComment();
						// 制作商品名称
						productNames.add(product.getOrderProductName());
					}
					attachmentDO.setComment(comment);
					attachmentDO.setProductNames(productNames.toArray(new String[productNames.size()]));
					attachmentDO.setStatus(BonConstant.PHOTO_STATUS_RC); // 普通入册/入册扉页，统一返回状态2：普通入册
				}
				doList.add(attachmentDO);
			}
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, doList);
		result.setModel(ResultDO.SECOND_MODEL_KEY, total > 0 ? total : doList.size()); // 照片张数
		result.setModel(ResultDO.THIRD_MODEL_KEY, notPickNum); // 未选照片
		return result;
	}

	@Override
	public ResultDO edit(WorkPhotoProductDO domain) {
		// TODO Auto-generated method stub
		return null;
	}

	/** 删除该照片的所有入册信息 */
	public int deleteByAttachmentID(Integer attachmentID, String businessNo) {
		WorkPhotoProductExample example = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria criteria = example.createCriteria();
		criteria.andBusinessNoEqualTo(businessNo);
		criteria.andAttachmentIDEqualTo(attachmentID);
		return workPhotoProductMapper.deleteByExample(example);
	}

	/** 删除制定商品的扉页（将扉页照片设置为普通入册） */
	public int removeCover(Integer productID, String businessNo) {
		WorkPhotoProductExample example = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria criteria = example.createCriteria();
		criteria.andOrderProductIDEqualTo(productID); // 商品
		criteria.andBusinessNoEqualTo(businessNo); // 销售业务单号
		criteria.andStatusEqualTo(BonConstant.PRODUCT_PHOTO_COVER);
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		WorkPhotoProduct entity = new WorkPhotoProduct();
		entity.setStatus(BonConstant.PRODUCT_PHOTO_NORMAL); // 普通入册
		return workPhotoProductMapper.updateByExampleSelective(entity, example);
	}

	public List<WorkPhotoProductDO> getWorkPhotoProductDOList(List<WorkPhotoProduct> list) {
		List<WorkPhotoProductDO> dstList = new ArrayList<WorkPhotoProductDO>();

		for (WorkPhotoProduct data : list) {
			WorkPhotoProductDO dst = getWorkPhotoProductDO(data);
			if (null != dst) {
				dstList.add(dst);
			}
		}
		return dstList;
	}

	public WorkPhotoProductDO getWorkPhotoProductDO(WorkPhotoProduct obj) {
		if (obj == null)
			return null;
		WorkPhotoProductDO dst = new WorkPhotoProductDO();
		BeanUtilsExtends.copyProperties(dst, obj);
		dst.setCreatetime(obj.getCreatetime().getTime() + "");
		if (null != obj.getModifitime()) {
			dst.setModifitime(obj.getModifitime().getTime() + "");
		}
		if (null != obj.getUploadDate()) {
			dst.setUploadDateStr(obj.getUploadDate().getTime() + "");
		}
		return dst;
	}

	@Override
	public ResultDO setCover(WorkPhotoProductDO domain) {
		ResultDO result = new ResultSupport();
		if (isNumberInValid(domain.getId())) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		// 查询入册数据
		WorkPhotoProduct entity = workPhotoProductMapper.selectByPrimaryKey(domain.getId());
		if (null == entity) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			return result;
		}

		// 删除该商品之前的入册扉页
		removeCover(entity.getOrderProductID(), entity.getBusinessNo());

		// 设置新扉页
		entity.setStatus(BonConstant.PRODUCT_PHOTO_COVER);
		int ret = workPhotoProductMapper.updateByPrimaryKeySelective(entity);
		if (ret < 1) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("扉页设置失败");
			return result;
		}

		return result;
	}

	@Override
	public ResultDO account(WorkPhotoProductDO domain) {

		ResultDO resultDO = new ResultSupport();
		// 参数验证：工单idID, 销售业务单号，照片id
		if (isNumberInValid(domain.getWorkOrderID()) || isNumberInValid(domain.getAttachmentID())
				|| StringUtils.isEmpty(domain.getBusinessNo())) {
			resultDO.setSuccess(false);
			resultDO.setErrorCode(ResultDO.PARAM_ERROR);
			resultDO.setErrorMsg("参数错误");
			return resultDO;
		}

		// 查询订单商品列表
		resultDO = queryProduct(domain);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}

		// 查询照片入册信息
		WorkPhotoProductExample example = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria criteria = example.createCriteria();
		criteria.andAttachmentIDEqualTo(domain.getAttachmentID());
		criteria.andBusinessNoEqualTo(domain.getBusinessNo());
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		List<WorkPhotoProduct> productList = workPhotoProductMapper.selectByExample(example);
		Map<Integer, WorkPhotoProduct> map = new HashMap<Integer, WorkPhotoProduct>();
		for (WorkPhotoProduct data : productList) {
			map.put(data.getOrderProductID(), data);
		}

		List<OrderProductDO> productDOList = (List<OrderProductDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY);

		// 查询入底备注
		WorkPhotoFilmExample filmExample = new WorkPhotoFilmExample();
		WorkPhotoFilmExample.Criteria filmCriteria = filmExample.createCriteria();
		filmCriteria.andAttachmentIDEqualTo(domain.getAttachmentID());
		filmCriteria.andBusinessNoEqualTo(domain.getBusinessNo());
		filmCriteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
		filmExample.setOrderByClause(" id desc ");
		List<WorkPhotoFilm> filmList = workPhotoFilmMapper.selectByExample(filmExample);

		String comment = "";
		if (null != filmList && filmList.size() > 0) {
			comment = filmList.get(0).getComment();
		}

		for (OrderProductDO data : productDOList) {
			data.setAttachmentID(domain.getAttachmentID());
			if (data.getRdFlag() == 0) {
				// 普通商品
				data.setPhotoNum(countByProduct(data.getOrderProductID(), domain.getBusinessNo()));
				// 默认为未入册、不是扉页
				data.setStatus(BonConstant.STATUS_FALSE); // 默认未选
				if (map.containsKey(data.getOrderProductID())) {
					WorkPhotoProduct product = map.get(data.getOrderProductID());
					// 入册本商品
					data.setStatus(product.getStatus());
					// 制作要求
					comment = product.getComment();
				}
			} else {
				// 入底数据
				data.setPhotoNum(countRdPhoto(data.getOrderProductID(), domain.getBusinessNo()));
				// 默认入底
				data.setStatus(BonConstant.STATUS_TRUE);
			}
		}
		resultDO.setModel(ResultDO.FIRST_MODEL_KEY, productDOList);
		resultDO.setModel(ResultDO.SECOND_MODEL_KEY, comment);
		return resultDO;
	}

	@Override
	public ResultDO queryProduct(WorkPhotoProductDO domain) {
		// TODO： 查询订单商品列表（测试用）
		ResultDO result = new ResultSupport();
		if (isNumberInValid(domain.getWorkOrderID())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		// 查询工单信息
		ResultDO resultDO = workOrderService.get(domain.getWorkOrderID());
		if (!resultDO.isSuccess()) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("工单信息查询失败");
			result.setSuccess(false);
			return result;
		}
		WorkOrderDO workOrderDO = (WorkOrderDO) resultDO.getModel(ResultDO.FIRST_MODEL_KEY);

		// TODO: 测试数据
		result.setModel(ResultDO.FIRST_MODEL_KEY,
				getProductList(workOrderDO.getId(), workOrderDO.getOrderID(), workOrderDO.getOrderProductSceenID()));
		return result;
	}

	/** 查询某个订单商品入册的数量 */
	public Integer countByProduct(Integer orderProductID, String businessNo) {

		WorkPhotoProductExample example = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria criteria = example.createCriteria();
		criteria.andOrderProductIDEqualTo(orderProductID);
		criteria.andBusinessNoEqualTo(businessNo);
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		return workPhotoProductMapper.countByExample(example);
	}

	/** 查询本次已入底照片张数（客户自备底片不计入） */
	public Integer countRdPhoto(Integer orderProductID, String businessNo) {

		WorkPhotoFilmExample example = new WorkPhotoFilmExample();
		WorkPhotoFilmExample.Criteria criteria = example.createCriteria();
		criteria.andWorkOrderIDEqualTo(orderProductID);
		criteria.andBusinessNoEqualTo(businessNo);
		criteria.andSourceNotEqualTo(BonConstant.PHOTO_SOURCE_CUSTOMER); // 片源(不包含自备底片)
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL); // 未删除
		criteria.andStatusEqualTo(BonConstant.STATUS_TRUE); // 选中

		return workPhotoFilmMapper.countByExample(example);
	}

	/** 初始化选片数据 */
	public ResultDO initPhotoFilm(Integer workOrderID, Integer workPhotographyID, String businessNo,
			String lastBusinessNo, Integer source) {

		ResultDO result = new ResultSupport();
		// 1. 数据验证(工单id、业务单号)
		if (isNumberInValid(workOrderID) || isNumberInValid(workPhotographyID) || StringUtils.isEmpty(businessNo)) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}

		Map<Integer, WorkPhotoFilm> filmMap = new HashMap<Integer, WorkPhotoFilm>();
		if (null != lastBusinessNo) {
			// 查询原业务单号的所有照片，并生成map
			WorkPhotoFilmExample filmExample = new WorkPhotoFilmExample();
			WorkPhotoFilmExample.Criteria filmCriteria = filmExample.createCriteria();
			filmCriteria.andWorkOrderIDEqualTo(workOrderID);
			filmCriteria.andBusinessNoEqualTo(lastBusinessNo);

			List<WorkPhotoFilm> list = workPhotoFilmMapper.selectByExample(filmExample);
			for (WorkPhotoFilm data : list) {
				filmMap.put(data.getAttachmentID(), data);
			}
		}

		// 查询所有照片
		WorkAttachmentExample example = new WorkAttachmentExample();
		WorkAttachmentExample.Criteria criteria = example.createCriteria();
		criteria.andWorkOrderIDEqualTo(workOrderID);
		criteria.andWorkPhotographyIDEqualTo(workPhotographyID);
		criteria.andAttachTypeEqualTo(BonConstant.ATTACHMENT_TYPE_PHOTO);
		criteria.andSourceIn(Arrays.asList(new Integer[] { source, BonConstant.PHOTO_SOURCE_CUSTOMER }));
		example.setOrderByClause(" id desc ");

		List<WorkAttachment> attachmentList = workAttachmentMapper.selectByExample(example);

		List<WorkPhotoFilm> filmList = new ArrayList<WorkPhotoFilm>();
		for (WorkAttachment data : attachmentList) {
			if (filmMap.containsKey(data.getId())) {
				// 判断入底数据是否存在，若存在且需要复制数据时，则沿用上一次的数据，修改id和业务单号
				WorkPhotoFilm film = filmMap.get(data.getId());
				if (!businessNo.equals(lastBusinessNo)) {
					film.setId(null);
					film.setBusinessNo(businessNo);
					filmList.add(film);
				}
			} else {
				// 不存在，则从照片源导入入底数据
				WorkPhotoFilm film = new WorkPhotoFilm();
				film.setAttachmentID(data.getId());
				film.setBusinessNo(businessNo);
				film.setWorkOrderID(workOrderID);
				film.setSource(data.getSource());
				film.setStatus(BonConstant.STATUS_TRUE); // 默认选中（入底）
				film.setIsDelete(data.getIsDelete());
				filmList.add(film);
			}
		}
		if (filmList.size() > 0) {
			int ret = workPhotoFilmMapper.insertBatch(filmList);
			if (ret < 1) {
				// 入底数据导入失败
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("数据初始化失败");
				result.setSuccess(false);
				return result;
			}
		}
		return result;
	}

	/**
	 * 统计所有订单商品的入册照片数量及入底数量 <b>Description:统计所有商品的入册入底数据，提供给订单模块，更新订单相关数据</b>
	 * <br>
	 * 
	 * @param workOrderID
	 * @param businessNo
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年10月8日 下午4:10:43 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Override
	public ResultDO accountPick(Integer workOrderID, String businessNo) {

		ResultDO result = new ResultSupport();
		// 1. 数据验证(工单id、业务单号)
		if (isNumberInValid(workOrderID) || StringUtils.isEmpty(businessNo)) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}

		// 查询工单
		ResultDO resultDO = workOrderService.get(workOrderID);
		if (!resultDO.isSuccess()) {
			return resultDO;
		}

		WorkOrderDO orderDO = (WorkOrderDO) resultDO.getModel(ResultDO.FIRST_MODEL_KEY);

		WorkPhotoPickDO pickDO = new WorkPhotoPickDO();
		pickDO.setBusinessNo(businessNo);

		// 查询订单商品及入册数据
		List<OrderProductDO> productList = getPickProducts(workOrderID, businessNo);

		// 查询入底数（需要算价的底片，不包含自备底片）
		WorkPhotoFilmExample filmExample = new WorkPhotoFilmExample();
		WorkPhotoFilmExample.Criteria filmCriteria = filmExample.createCriteria();
		filmCriteria.andWorkOrderIDEqualTo(workOrderID);
		filmCriteria.andBusinessNoEqualTo(businessNo);
		filmCriteria.andSourceNotEqualTo(BonConstant.PHOTO_SOURCE_CUSTOMER);
		filmCriteria.andStatusEqualTo(BonConstant.STATUS_TRUE);
		filmCriteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		int rdCount = workPhotoFilmMapper.countByExample(filmExample);
		OrderProductDO data = new OrderProductDO();
		data.setOrderID(orderDO.getId()); // 订单ID
		data.setOrderProductSceenID(orderDO.getOrderProductSceenID()); // 场次ID
		data.setBusinessNo(businessNo);
		data.setOrderProductID(0); // TODO： 入底商品ID???
		data.setPhotoNum(rdCount);
		productList.add(data);

		pickDO.setProductList(productList);

		result.setModel(ResultDO.FIRST_MODEL_KEY, pickDO);
		return result;
	}

	@Override
	public ResultDO pickDetail(WorkPhotoProductDO domain) {

		ResultDO result = new ResultSupport();
		// 1. 数据验证(工单id、业务单号)
		if (isNumberInValid(domain.getWorkOrderID()) || StringUtils.isEmpty(domain.getBusinessNo())) {
			result.setErrorCode(ResultDO.PARAM_ERROR);
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}

		WorkPickDetailDO detailDO = new WorkPickDetailDO();
		detailDO.setWorkOrderID(domain.getWorkOrderID());
		detailDO.setBusinessNo(domain.getBusinessNo());

		// 2. 查询入底照片数量
		WorkPhotoFilmExample filmExample = new WorkPhotoFilmExample();
		WorkPhotoFilmExample.Criteria filmCriteria = filmExample.createCriteria();
		filmCriteria.andWorkOrderIDEqualTo(domain.getWorkOrderID());
		filmCriteria.andBusinessNoEqualTo(domain.getBusinessNo());
		filmCriteria.andStatusEqualTo(BonConstant.STATUS_TRUE);
		filmCriteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);

		List<WorkPhotoFilm> filmList = workPhotoFilmMapper.selectByExample(filmExample);
		List<Integer> ids = new ArrayList<Integer>();
		for (WorkPhotoFilm data : filmList) {
			ids.add(data.getAttachmentID());
		}
		// 查询照片源数据
		WorkAttachmentExample attachmentExample = new WorkAttachmentExample();
		WorkAttachmentExample.Criteria attachmentCriteria = attachmentExample.createCriteria();
		attachmentCriteria.andIdIn(ids);

		List<WorkAttachment> attachments = workAttachmentMapper.selectByExample(attachmentExample);
		// 所选入底照片数据（包含自备底片）
		detailDO.setPickList(getWorkAttachmentDOList(attachments));
		detailDO.setPhotoNum(detailDO.getPickList().size());

		// TODO 3. 查询入册商品信息（从订单模块接口返回商品）（问题：入册商品的数量 未知）
		detailDO.setProductList(getPickProducts(domain.getWorkOrderID(), domain.getBusinessNo()));
		// 待制作的商品数量
		detailDO.setProductNum(detailDO.getProductList().size());
		result.setModel(ResultDO.FIRST_MODEL_KEY, detailDO);

		return result;
	}

	public List<OrderProductDO> getPickProducts(Integer workOrderID, String businessNo) {

		WorkPhotoProductExample example = new WorkPhotoProductExample();
		WorkPhotoProductExample.Criteria criteria = example.createCriteria();
		criteria.andWorkOrderIDEqualTo(workOrderID);
		criteria.andBusinessNoEqualTo(businessNo);
		criteria.andIsDeleteEqualTo(BonConstant.DB_NORMAL);
		example.setOrderByClause(" orderProductID asc");

		List<WorkPhotoProduct> list = workPhotoProductMapper.selectByExample(example);
		List<OrderProductDO> productList = new ArrayList<OrderProductDO>();

		if (null != list && list.size() > 0) {
			// 入册商品
			Integer productID = list.get(0).getOrderProductID();
			List<WorkPhotoProductDO> productPickList = new ArrayList<WorkPhotoProductDO>();
			for (int i = 0; i < list.size(); i++) {
				if (productID != list.get(i).getOrderProductID()) {
					OrderProductDO data = new OrderProductDO();
					data.setWorkOrderID(workOrderID); // 订单ID
					data.setOrderID(productPickList.get(0).getId()); // 订单ID
					data.setOrderProductSceenID(productPickList.get(0).getOrderProductSceenID()); // 场次ID
					data.setOrderProductID(productID); // 商品ID
					data.setOrderProductName(productPickList.get(0).getOrderProductName()); // 商品名称
					data.setPhotoNum(productPickList.size()); // 商品入册照片数量
					data.setBusinessNo(businessNo);
					data.setPickList(productPickList);
					productList.add(data);

					productID = list.get(i).getOrderProductID();
					productPickList = new ArrayList<WorkPhotoProductDO>();
				}
				productPickList.add(getWorkPhotoProductDO(list.get(i)));
			}

			OrderProductDO data = new OrderProductDO();
			data.setOrderID(productPickList.get(0).getId()); // 订单ID
			data.setOrderProductSceenID(productPickList.get(0).getOrderProductSceenID()); // 场次ID
			data.setOrderProductID(productID); // 商品ID
			data.setOrderProductName(productPickList.get(0).getOrderProductName()); // 商品名称
			data.setPhotoNum(productPickList.size()); // 商品入册照片数量
			data.setBusinessNo(businessNo);
			data.setPickList(productPickList);
			productList.add(data);
		}
		return productList;
	}
}
